package com.bye.xiaomi_mall.service.impl;

import java.util.List;

import com.bye.xiaomi_mall.domain.TdGoodsSpecifications;
import com.bye.xiaomi_mall.mapper.TdCouponMapper;
import com.bye.xiaomi_mall.mapper.TdGoodsMapper;
import com.bye.xiaomi_mall.mapper.TdGoodsSpecificationsMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bye.xiaomi_mall.mapper.TdGoodsCategoryMapper;
import com.bye.xiaomi_mall.domain.TdGoodsCategory;
import com.bye.xiaomi_mall.service.ITdGoodsCategoryService;

/**
 * 商品类别Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@Service
public class TdGoodsCategoryServiceImpl implements ITdGoodsCategoryService {
    @Autowired
    private TdGoodsCategoryMapper tdGoodsCategoryMapper;

    @Autowired
    private TdGoodsMapper tdGoodsMapper;

    @Autowired
    private TdCouponMapper tdCouponMapper;


    /**
     * 查询商品类别
     *
     * @param id 商品类别主键
     * @return 商品类别
     */
    @Override
    public TdGoodsCategory selectTdGoodsCategoryById(Long id) {
        return tdGoodsCategoryMapper.selectTdGoodsCategoryById(id);
    }

    /**
     * 查询商品类别列表
     *
     * @param tdGoodsCategory 商品类别
     * @return 商品类别
     */
    @Override
    public List<TdGoodsCategory> selectTdGoodsCategoryList(TdGoodsCategory tdGoodsCategory) {
        return tdGoodsCategoryMapper.selectTdGoodsCategoryList(tdGoodsCategory);
    }

    /**
     * 新增商品类别
     *
     * @param tdGoodsCategory 商品类别
     * @return 结果
     */
    @Override
    public int insertTdGoodsCategory(TdGoodsCategory tdGoodsCategory) {
        tdGoodsCategory.setCreateTime(DateUtils.getNowDate());
        return tdGoodsCategoryMapper.insertTdGoodsCategory(tdGoodsCategory);
    }

    /**
     * 修改商品类别
     *
     * @param tdGoodsCategory 商品类别
     * @return 结果
     */
    @Override
    public int updateTdGoodsCategory(TdGoodsCategory tdGoodsCategory) {
        return tdGoodsCategoryMapper.updateTdGoodsCategory(tdGoodsCategory);
    }

    /**
     * 批量删除商品类别
     *
     * @param ids 需要删除的商品类别主键
     * @return 结果
     */
    @Override
    public int deleteTdGoodsCategoryByIds(Long[] ids) {

        for (Long id : ids) {
            Integer i = selectNum(id);
            if (i != 0) {
                return i;
            }
        }
        return tdGoodsCategoryMapper.deleteTdGoodsCategoryByIds(ids);
    }

    /**
     * 删除商品类别信息
     *
     * @param id 商品类别主键
     * @return 结果
     */
    @Override
    public int deleteTdGoodsCategoryById(Long id) {
        Integer i = selectNum(id);
        if (i != 0) {
            return i;
        }
        return tdGoodsCategoryMapper.deleteTdGoodsCategoryById(id);
    }

    @Override
    public List<TdGoodsCategory> selectGoodsSp(String status) {
        return tdGoodsCategoryMapper.selectGoodsSp(status);
    }


    public Integer selectNum(Long categoryId) {
        int a = tdGoodsMapper.selectTdGoodsByCat(categoryId);
        if (a != 0) return -1;
        int b = tdCouponMapper.selectTdGoodsByCat(categoryId);
        if (b != 0) return -2;
        return 0;
    }
}
